package edu.ccut.saturn.manager.bundles.core;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Dictionary;

import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.core.ServiceFactory;

public class UninstallBundle extends DefaultComponent {
	
	private static final String BUNDLE_NAME_SEPARATOR = "\\";
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		String key = (String) parameterContext.getParameterValue(0);
		String location = "";
		Bundle[] bundles = ServiceFactory.getBundleService().getBundles();
		Bundle insertBundle = null;

		for (Bundle bundle : bundles) {
			if (bundle.getSymbolicName().equals(key)) {
				location = bundle.getLocation();
				insertBundle = bundle;
				bundle.uninstall();
				break;
			}
		}
		Dictionary dictionary = insertBundle.getHeaders();
		String version = dictionary.get("Bundle-Version").toString();
		String name = dictionary.get("Bundle-Name").toString();
		String status = "disable";
		String url = insertBundle.getLocation();
		String bundleFileName = "";
		String bundleFilePath = "";
		if (url.indexOf(BUNDLE_NAME_SEPARATOR) != -1) {
			bundleFileName = url.substring(url
					.lastIndexOf(BUNDLE_NAME_SEPARATOR) + 1);

			bundleFilePath = url.substring(0, url
					.lastIndexOf(BUNDLE_NAME_SEPARATOR));

		} else {
			bundleFileName = url;
		}
		String sql = null;
		
	/*	if("mysql".equalsIgnoreCase(getDBType(parameterContext))) {
			sql = "insert into bundlesInfo (symbolicName, name, version, status, bundleFilePath, bundleFileName) values (?,?,?,?,?,? )";
		}

		if("oracle".equalsIgnoreCase(getDBType(parameterContext))) {
			sql = "insert into bundlesInfo (id, symbolicName, name, version, status, bundleFilePath, bundleFileName) values ( SATURN_SEQUENCE_BUNDLEINFO.Nextval,?,?,?,?,?,? )"; 
		}
		
		if("Microsoft SQL Server".equalsIgnoreCase(getDBType(parameterContext))) {
			sql = "insert into bundlesInfo (symbolicName, name, version, status, bundleFilePath, bundleFileName) values (?,?,?,?,?,? )"; 
		}*/
		


		if("oracle".equalsIgnoreCase(getDBType(parameterContext))) {
			sql = "insert into bundlesInfo (id, symbolicName, name, version, status, bundleFilePath, bundleFileName) values ( SATURN_SEQUENCE_BUNDLEINFO.Nextval,?,?,?,?,?,? )"; 
		} else {
			sql = "insert into bundlesInfo (symbolicName, name, version, status, bundleFilePath, bundleFileName) values (?,?,?,?,?,? )"; 
		}
		
		//String sql = "insert into bundlesInfo (id, symbolicName, name, version, status, bundleFilePath, bundleFileName) values ( SATURN_SEQUENCE_BUNDLEINFO.Nextval,?,?,?,?,?,? )";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = parameterContext.getConnection()
					.prepareStatement(sql);

			preparedStatement.setString(1, key);
			preparedStatement.setString(2, name);
			preparedStatement.setString(3, version);
			preparedStatement.setString(4, status);
			preparedStatement.setString(5, bundleFilePath);
			preparedStatement.setString(6, bundleFileName);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			ServiceFactory.getBundleService().getBundleContext().installBundle(location);
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
		}
	}
	
	private String getDBType(IParameterContext parameterContext) {
		
		String dbType = null;
		
		try {
			dbType = (parameterContext.getConnection().getMetaData()).getDatabaseProductName();
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error("CONNENT ERROR! ");
		}
		
		if(dbType == null) {
			return null;
		}
		
		return dbType;		
	}
}